﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.BaoCao
{
    public partial class TinhTrangHoatDong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindPhongHoc(cboPhongHoc);
                BindCatalog.BindLoaiTinhTrang(cboLoaiTinhTrang);
                cmdLuu.Enabled = false;
            }
        }

        protected void rpThietBi1_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_MaThietBi.Value = id.ToString();
                litTenThietBi.Text = "Tên thiết bị: <font color=red>" + DataRepository.ThietBiProvider.GetByMaThietBi(id).TenThietBi + "</font>";
                cmdLuu.Enabled = true;
                kiemTraHong(H_MaThietBi.Value);
            }
        }

        public void kiemTraHong(string maThietBi)
        {
            int totalRow = 0;
            string whereClause = "";
            whereClause = "MaThietBi=" + maThietBi + " and TrangThai=0";
            TList<ThietBi> TList_ThietBi = DataRepository.ThietBiProvider.GetPaged(whereClause, "", 0, 0, out totalRow);
            if (TList_ThietBi.Count > 0)
            {
                cmdLuu.Enabled = false;
                WebMessage.Show("Thiết bị đã được báo cáo hoặc đang trong quá trình sửa chữa! Mời chọn thiết bị khác!");
            }
        }
        protected void rpThietBi2_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_MaThietBi.Value = id.ToString();
                litTenThietBi.Text = "Tên thiết bị: <font color=red>" + DataRepository.ThietBiProvider.GetByMaThietBi(id).TenThietBi + "</font>";
                cmdLuu.Enabled = true;
                kiemTraHong(H_MaThietBi.Value);
                
            }
        }
        protected void rpThietBi3_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_MaThietBi.Value = id.ToString();
                litTenThietBi.Text = "Tên thiết bị: <font color=red>" + DataRepository.ThietBiProvider.GetByMaThietBi(id).TenThietBi + "</font>";
                cmdLuu.Enabled = true;
                kiemTraHong(H_MaThietBi.Value);
                
            }
        }
        protected void rpThietBi4_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_MaThietBi.Value = id.ToString();
                litTenThietBi.Text = "Tên thiết bị: <font color=red>" + DataRepository.ThietBiProvider.GetByMaThietBi(id).TenThietBi + "</font>";
                cmdLuu.Enabled = true;
                kiemTraHong(H_MaThietBi.Value);
            }
        }

        protected void cmdReport_OnClick(object sender, EventArgs e)
        {
            //int totalRow = 0;
            //string whereClause = "MaThietBi=" + H_MaThietBi.Value + " and MaThietBi in (select MaThietBi from ThietBi where TrangThai=0)";
            //TList<HoatDongSuaChua> TList_HoatDong = DataRepository.HoatDongSuaChuaProvider.GetPaged(whereClause, "", 0,
            //    0, out totalRow);
            //if (TList_HoatDong.Count > 0)
            //{
            //    WebMessage.Show("Báo cáo thất bại! Thiết bị này vẫn đang chờ giải quyết!");
            //}
            bool status = false;
            HoatDongSuaChua ent_SuaChua = new HoatDongSuaChua();
            ent_SuaChua.MaThietBi = Convert.ToInt32(H_MaThietBi.Value);
            ent_SuaChua.MaLoaiTinhTrang = Convert.ToInt32(cboLoaiTinhTrang.SelectedValue);
            ent_SuaChua.TrangThai = false;
            ent_SuaChua.NgayCapNhat = Common.GetServerDateTime();
            ent_SuaChua.GhiChu = txtGhiChu.Text;
            ent_SuaChua.MaCanBo = Common.GetKeyUserInfo("Id");
            status = DataRepository.HoatDongSuaChuaProvider.Insert(ent_SuaChua);
            if (!status)
            {
                WebMessage.Show("Báo cáo thất bại! Xem lại tiến trình!");
            }
            else
            {
                
                string query = string.Empty;

                conn.Open();
                query = "UPDATE ThietBi SET TrangThai = 0 where MaThietBi = '" + H_MaThietBi.Value + "'";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
                cboLoaiTinhTrang.SelectedValue = "0";
                txtGhiChu.Text = "";
                H_MaThietBi.Value = "";
                BindThietBi(cboPhongHoc.SelectedValue);
                Page_Load(sender,e);
                WebMessage.Show("Báo cáo thành công! Cán bộ Quản lý sẽ kiểm tra thiết bị và báo cáo lại sau thời gian từ 3 đến 5 ngày. Xin cảm ơn!");
                cmdLuu.Enabled = false;
            }
        }

        protected void cboPhongHoc_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            litPhong.Text = "của " + "<font color=red>Phòng máy tính " + cboPhongHoc.SelectedValue + "</font>("+DataRepository.ThietBiProvider.Find("MaPhong="+cboPhongHoc.SelectedValue).Count+" máy)";
            BindThietBi(cboPhongHoc.SelectedValue);
            PhongHoc entPhongHoc =
                    DataRepository.PhongHocProvider.GetByMaPhong(Convert.ToInt32(cboPhongHoc.SelectedValue));
            if (entPhongHoc != null)
            {
                litCanBo.Text = "<font color=green>Cán bộ Quản lý " + entPhongHoc.TenPhong + ": " + "<b>" +
                                DataRepository.CanBoProvider.GetByMaCanBo(entPhongHoc.MaCanBo).HoTen + "</b></font>";
            }
        }

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        public void chuyenDoi(string str1, string str2)
        {
            string test = "";
            str1 = str1.Replace("\"", "");
            str1 = str1.Replace("{", "");
            str1 = str1.Replace("}", "");
            str1 = str1.Replace("[", "");
            str1 = str1.Replace("]", "");
            str1 = str1.Replace("id:", "");
            string[] arrayStr1 = str1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);//Vị trí
            string[] arrayStr2 = str2.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);//Mã thiết bị
            if (str1.Length > 0)
            {
                for (int i = 0; i <= arrayStr1.Length - 1; i++)
                {

                    string query = string.Empty;

                    conn.Open();
                    query = "UPDATE ThietBi SET ViTri = '" + arrayStr2[i] + "' where MaThietBi = '" + arrayStr1[i] + "'";
                    SqlCommand sqlCommand = new SqlCommand();
                    sqlCommand.Connection = conn;
                    sqlCommand.CommandText = query;
                    sqlCommand.ExecuteNonQuery();
                    conn.Close();

                }
            }
        }

        private void BindThietBi(string maPhong)
        {
            string whereClause = "";
            int totalRow = 0;
            if(maPhong!="0")
            whereClause += "MaPhong=" + maPhong;
            else
            {
                return;
            }
            TList<ThietBi> TList_ThietBi = DataRepository.ThietBiProvider.GetPaged(whereClause, "ViTri DESC", 0, 0, out totalRow);
            litday1.Text = "";
            litday2.Text = "";
            litday3.Text = "";
            litday4.Text = "";
            TList<ThietBi> TList_ThietBi1 = new TList<ThietBi>();
            for (int i = 0; i < 10; i++)
            {
                TList_ThietBi1.Add(TList_ThietBi[i]);

                if (i == 9)
                {
                    litday1.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday1.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay1.DataSource = TList_ThietBi1;
            rpDay1.DataBind();
            TList<ThietBi> TList_ThietBi2 = new TList<ThietBi>();
            for (int i = 10; i < 20; i++)
            {
                TList_ThietBi2.Add(TList_ThietBi[i]);
                if (i == 19)
                {
                    litday2.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday2.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay2.DataSource = TList_ThietBi2;
            rpDay2.DataBind();
            TList<ThietBi> TList_ThietBi3 = new TList<ThietBi>();
            for (int i = 20; i < 30; i++)
            {
                TList_ThietBi3.Add(TList_ThietBi[i]);
                if (i == 29)
                {
                    litday3.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday3.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay3.DataSource = TList_ThietBi3;
            rpDay3.DataBind();
            TList<ThietBi> TList_ThietBi4 = new TList<ThietBi>();
            for (int i = 30; i < 40; i++)
            {
                TList_ThietBi4.Add(TList_ThietBi[i]);
                if (i == 39)
                {
                    litday4.Text += TList_ThietBi[i].ViTri;
                }
                else
                {
                    litday4.Text += TList_ThietBi[i].ViTri + ",";
                }
            }
            rpDay4.DataSource = TList_ThietBi4;
            rpDay4.DataBind();

        }

        public string getHoverTrangThai(object ob)
        {
            int id = Convert.ToInt32(ob.ToString());
            TList<HoatDongSuaChua> TList_HoatDong = DataRepository.HoatDongSuaChuaProvider.Find("MaThietBi=" + id);
            if (TList_HoatDong.Count > 0)
            {
                return TList_HoatDong[0].GhiChu;
            }
            else
            {
                return "";
            }
        }
    }
}